user nginx;
worker_processes auto;

error_log /dev/stderr warn;
pid        /tmp/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    client_body_temp_path /tmp/client_temp;
    proxy_temp_path       /tmp/proxy_temp;
    fastcgi_temp_path     /tmp/fastcgi_temp;
    uwsgi_temp_path       /tmp/uwsgi_temp;
    scgi_temp_path        /tmp/scgi_temp;

    proxy_cache_path /cache keys_zone=dcache:10m max_size=200m inactive=30d;

    log_format json_combined escape=json '{ "time_local": "$time_local", '
        '"host": "$host", '
        '"remote_addr": "$remote_addr", '
        '"remote_user": "$remote_user", '
        '"request": "$request", '
        '"status": "$status", '
        '"body_bytes_sent": "$body_bytes_sent", '
        '"request_time": "$request_time", '
        '"http_referrer": "$http_referer", '
        '"http_user_agent": "$http_user_agent" }';

    keepalive_timeout  65;
    port_in_redirect off;
    ssi off;
    gzip off;

    map $host $lang {
        hostnames;
        "deckhouse.ru"   "ru";
        "*.deckhouse.ru"   "ru";
        # for test environments
        "deckhouse.ru.*" "ru";
        default       "en";
    }

    server {
        listen       8080;
        server_name  _;

        root   /app;
        index  readme.html index.html;
        error_page 403 404 /404.html;

        charset utf-8;
        access_log     /dev/stdout json_combined;

        location /link_test_report.txt {
          deny all;
        }

        location = /healthz {
            access_log  off;
            add_header Content-Type text/plain;
            return 200;
        }

        location / {
            try_files /$lang/$uri /$lang/$uri/index.html /$lang/$uri/ $uri $uri/index.html $uri/ =404;
        }

        location /modules {
            try_files /$lang$uri /$lang$uri/ =404;
        }

        location = /documentation/sitemap.xml {
            try_files /$lang/sitemap.xml =404;
        }

        location ~* ^/documentation/(.*)$  {
            return 301 https://$host/documentation/v1/$1;
        }

        location ~* ^/downloads/deckhouse-cli/v[0-9]+\.[0-9]+\.[0-9]+/d8-v[0-9]+\.[0-9]+\.[0-9]+-(darwin|linux)-(amd|arm)64.tar.gz$   {
            rewrite ^/downloads/deckhouse-cli/([^/]+/[^/]+\.gz)$ /deckhouse/deckhouse-cli/releases/download/$1/  break;

            proxy_cache             dcache;
            proxy_cache_key         $uri;
            proxy_cache_methods     GET;
            proxy_set_header        X-Real-IP         $remote_addr;
            proxy_set_header        X-Original-URI    $request_uri;
            proxy_set_header        X-Forwarded-For   $proxy_add_x_forwarded_for;
            proxy_buffer_size       16k;
            proxy_buffers           4 16k;
            proxy_ignore_headers    Set-Cookie;
            proxy_ignore_headers    Cache-Control;
            proxy_intercept_errors  on;

            error_page              301 302 307 = @handle_redirects;
            proxy_pass              https://github.com;
        }

        location @handle_redirects {
            set                     $original_uri  $uri;
            set                     $orig_loc      $upstream_http_location;

            resolver                d8-kube-dns.kube-system.svc.cluster.local;
            proxy_cache             dcache;
            proxy_cache_key         $original_uri;
            proxy_intercept_errors  on;
            proxy_buffer_size       16k;
            proxy_buffers           4 16k;
            proxy_ignore_headers    Set-Cookie;
            proxy_ignore_headers    Cache-Control;

            proxy_pass              $orig_loc;
        }

    }
}
